﻿using System;
using AnjLab.SyncIT.Core.Api;
using AnjLab.SyncIT.Core.Data.DotProject;
using NUnit.Framework;
using System.IO;

namespace AnjLab.SyncIT.Core.Tests.Data.DotProject
{
    [TestFixture]
    public class TestDotProjectUIParser
    {
        [Test]
        public void TestParseTodoList()
        {
            var source = new Source();

            var parser = new DotProjectUIParser(source);

            string[] tasks = parser.ParseMyTODOList("file:///"
                                   + AppDomain.CurrentDomain.BaseDirectory.Replace('\\', '/')
                                   + "/data/dotproject/dotProject1.htm");

            Assert.AreEqual(3, tasks.Length);
            Assert.AreEqual("351", tasks[0]);
            Assert.AreEqual("417", tasks[1]);
            Assert.AreEqual("419", tasks[2]);

            parser.ParseMyTODOList("file:///"
                                   + AppDomain.CurrentDomain.BaseDirectory.Replace('\\', '/')
                                   + "/data/dotproject/dotProject2.htm");

            Assert.AreEqual(3, tasks.Length);
            Assert.AreEqual("351", tasks[0]);
            Assert.AreEqual("417", tasks[1]);
            Assert.AreEqual("419", tasks[2]);

            parser.ParseMyTODOList("file:///"
                                   + AppDomain.CurrentDomain.BaseDirectory.Replace('\\', '/')
                                   + "/data/dotproject/dotProject3.htm");

            Assert.AreEqual(3, tasks.Length);
            Assert.AreEqual("351", tasks[0]);
            Assert.AreEqual("417", tasks[1]);
            Assert.AreEqual("419", tasks[2]);
        }

        [Test]
        public void TestParseTask()
        {
            var source = new Source();

            var parser = new DotProjectUIParser(source);

            var task = (Task) parser.ParseTaskURL("file:///"
                                           + AppDomain.CurrentDomain.BaseDirectory.Replace('\\', '/')
                                           + "/data/dotproject/dotProject_Task.htm");

            Assert.AreEqual("10", task.UserID);
            Assert.AreEqual("351", task.ID);
            Assert.AreEqual("Проект 1", task.ProjectName);
            Assert.AreEqual("197", task.ProjectID);
            Assert.AreEqual("Задача 1", task.Summary);
            Assert.AreEqual("Имя Фамилия", task.Owner);
            Assert.AreEqual("7", task.OwnerID);
            Assert.IsNotNull(task.ResponsibleUsers);
            Assert.AreEqual(2, task.ResponsibleUsers.Length);
            Assert.AreEqual("10", task.ResponsibleUsers[1].ID);
            Assert.AreEqual("Дмитрий Гусев", task.ResponsibleUsers[1].DisplayName);
            Assert.AreEqual("7", task.ResponsibleUsers[0].ID);
            Assert.AreEqual("Имя Фамилия", task.ResponsibleUsers[0].DisplayName);
            Assert.AreEqual("нормальная", task.Priority);
            Assert.AreEqual(90, task.Progress);
            Assert.AreEqual("20/09/2007", task.StartDate);
            Assert.AreEqual("03/03/2008", task.EndDate);
            Assert.AreEqual("описание...", task.Description);
            Assert.AreEqual("Активная", task.Status);
            Assert.AreEqual(Importance.Normal, task.Importance);
            Assert.AreEqual(StatusCode.Active, task.StatusCode);
        }

        [Test]
        public void TestParseSettings()
        {
            var source = new Source();

            var parser = new DotProjectUIParser(source);

            var shortDateFormat = parser.ParseShortDateFormat("file:///"
                                                              + AppDomain.CurrentDomain.BaseDirectory.Replace('\\', '/')
                                                              + "/data/dotproject/dotProject_Settings.htm");

            Assert.AreEqual("%d/%m/%Y", shortDateFormat);
        }

        [Test]
        public void TestParseSettings212()
        {
            var source = new Source();

            var parser = new DotProjectUIParser(source);

            var shortDateFormat = parser.ParseShortDateFormat("file:///"
                                                              + AppDomain.CurrentDomain.BaseDirectory.Replace('\\', '/')
                                                              + "/data/dotproject/dp212-user-settings.htm");

            Assert.AreEqual("%m/%d/%Y", shortDateFormat);
        }

        [Test]
        public void TestConvertPhpFormatToDotNetFormat()
        {
            var result = DotProjectUIParser.ConvertPhpToDotNetDateFormat("%d/%m/%Y");

            Assert.AreEqual("dd/MM/yyyy", result);

            var d1 = new DateTime(2008, 12, 31);

            var s = d1.ToString(result, Library.InvariantCulture);
            
            Assert.AreEqual("31/12/2008", s);

            var d2 = DateTime.ParseExact(s, result, Library.InvariantCulture);

            Assert.AreEqual(d1, d2);
        }

        [Test]
        public void ParseDate()
        {
            var task = new Task
            {
                ShortDateFormat = DotProjectUIParser.ConvertPhpToDotNetDateFormat("%d/%m/%Y")
            };

            Assert.AreEqual("dd/MM/yyyy", task.ShortDateFormat);

            var d1 = task.ParseDotProjectDate("05/06/2008");

            Assert.AreEqual(5, d1.Day);
            Assert.AreEqual(6, d1.Month);
            Assert.AreEqual(2008, d1.Year);

            task = new Task
            {
                ShortDateFormat = DotProjectUIParser.ConvertPhpToDotNetDateFormat("%m/%d/%Y")
            };

            d1 = task.ParseDotProjectDate("05/06/2008");

            Assert.AreEqual(6, d1.Day);
            Assert.AreEqual(5, d1.Month);
            Assert.AreEqual(2008, d1.Year);
        }

        [Test]
        public void DateToString()
        {
            var task = new Task
            {
                ShortDateFormat = DotProjectUIParser.ConvertPhpToDotNetDateFormat("%b/%d/%Y")
            };

            var d1 = new DateTime(2008, 5, 6);

            var s = task.DateToDotProjectDateString(d1);

            Assert.AreEqual("May/06/2008", s);

            task = new Task
            {
                ShortDateFormat = DotProjectUIParser.ConvertPhpToDotNetDateFormat("%d/%m/%Y")
            };

            d1 = new DateTime(2008, 5, 6);

            s = task.DateToDotProjectDateString(d1);

            Assert.AreEqual("06/05/2008", s);

            task = new Task
            {
                ShortDateFormat = DotProjectUIParser.ConvertPhpToDotNetDateFormat("%m/%d/%Y")
            };

            d1 = new DateTime(2008, 5, 6);

            s = task.DateToDotProjectDateString(d1);

            Assert.AreEqual("05/06/2008", s);

            task = new Task
            {
                ShortDateFormat = DotProjectUIParser.ConvertPhpToDotNetDateFormat("%d/%b/%Y")
            };

            d1 = new DateTime(2008, 5, 6);

            s = task.DateToDotProjectDateString(d1);

            Assert.AreEqual("06/May/2008", s);
        }

        [Test]
        public void TestPingDotProject2_1_2()
        {
            var html = new StreamReader("Data/DotProject/dotProject-2.1.2-mytasks.htm").ReadToEnd();

            DotProjectUIParser.PingMyTodoList(html);

            Assert.That(true);
        }

        [Test]
        public void TestParseMyTODOList2_1_2()
        {
            var source = new Source();

            var parser = new DotProjectUIParser(source);

            string[] tasks = parser.ParseMyTODOList("file:///"
                                   + AppDomain.CurrentDomain.BaseDirectory.Replace('\\', '/')
                                   + "/data/dotproject/dotProject-2.1.2-mytasks.htm");

            Assert.AreEqual(3, tasks.Length);

            foreach (string task in tasks)
            {
                Console.WriteLine(task);
            }
        }

        [Test]
        public void TestParseMyTODOList2_1_5()
        {
            var source = new Source();

            var parser = new DotProjectUIParser(source);

            string[] tasks = parser.ParseMyTODOList("file:///"
                                   + AppDomain.CurrentDomain.BaseDirectory.Replace('\\', '/')
                                   + "/data/dotproject/dotProject-2.1.5-mytasks.htm");

            Assert.AreEqual(6, tasks.Length);

            foreach (string task in tasks)
            {
                Console.WriteLine(task);
            }
        }
    }
}
